package cn.tedu.restaurant.controller;


import cn.tedu.restaurant.common.JsonResult;
import cn.tedu.restaurant.entity.Dishes;
import cn.tedu.restaurant.service.DishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author JachinLin
 * @since 2020-10-05
 */
@RestController
@RequestMapping("/dishes")
public class DishesController {

    @Autowired
    @Resource
    private DishesService dishesService;

    private static final Integer SUCCESS=200;

    @RequestMapping("list")
    public JsonResult<List<Dishes>> listDishes() {
        JsonResult<List<Dishes>> result = new JsonResult<List<Dishes>>(SUCCESS);
        result.setData(dishesService.getAllDishes());
        return result;
    }

    @RequestMapping("type")
    public JsonResult<List<Dishes>> listDishesByType(@RequestParam("type") Integer type) {
        JsonResult<List<Dishes>> result = new JsonResult<List<Dishes>>(SUCCESS);
        result.setData(dishesService.getDishesByType(type));
        return result;
    }

    @RequestMapping("childType")
    public JsonResult<List<Dishes>> listDishesByChildType(@RequestParam("childType") Integer childtype) {
        JsonResult<List<Dishes>> result = new JsonResult<List<Dishes>>(SUCCESS);
        result.setData(dishesService.getDishesByChildType(childtype));
        return result;
    }

    @RequestMapping("search")
    public JsonResult<List<Dishes>> listDishesByName(@RequestParam("name") String name) {
        JsonResult<List<Dishes>> result = new JsonResult<List<Dishes>>(SUCCESS);
        result.setData(dishesService.getDishesByName(name));
        return result;
    }

    @RequestMapping("top")
    public JsonResult<Integer> top(@RequestParam("id") Integer id, HttpSession session) {
        JsonResult<Integer> result = new JsonResult<Integer>(SUCCESS);
        Integer uid = Integer.valueOf(session.getAttribute("uid").toString());
        result.setData(dishesService.updateTop(uid,1,id));
        return result;
    }
    @RequestMapping("byId")
    public JsonResult<Dishes> listById(@RequestParam("id") Integer id, HttpSession session) {
        JsonResult<Dishes> result = new JsonResult<Dishes>(SUCCESS);
        result.setData(dishesService.selectByDid(id));
        return result;
    }
}
